import java.io.*;
import java.net.Socket;


public class Input extends Thread {
	private Socket socket;
	private String message;
	private IRCParser parser;
	private IRCClient client;

	
	public Input(IRCClient client){
		this.client = client;
		parser = new IRCParser();
	}
	
	public void run(){
		while(true){
			if (socket != null && socket.isConnected()){
				try{
					InputStream input = socket.getInputStream();
					BufferedReader in = new BufferedReader(new InputStreamReader(input));
					message = in.readLine();
					while(message != null){
						IRCCommand cmd = parser.parse(message);
						client.evalCommand(cmd);
						message = in.readLine();
					}
					
				}
				catch (IOException e) {
					//e.printStackTrace();
				}
			}else if(!socket.isConnected()){
				client.onDisconnect();
			}
		}
	}
	
	public void setSocket(Socket socket) {
		this.socket = socket;
	}
	

}
